<template>
  <div class="page-category">
    <el-form @submit.prevent>
      <el-form-item>
        <el-alert
          :title="$t('publics.alertMessage1')"
          type="warning"
          show-icon
          :closable="false"
        />
      </el-form-item>
    </el-form>
    <el-form
      :inline="true"
      @submit.prevent
    >
      <el-form-item>
        <div
          v-if="isAuth('prod:category:save')"
          class="default-btn primary-btn"
          @click="onAddOrUpdate()"
        >
          {{ $t('crud.addTitle') }}
        </div>
      </el-form-item>
    </el-form>

    <el-table
      ref="categoryTableRef"
      :data="dataList"
      header-cell-class-name="table-header"
      row-class-name="table-row"
      row-key="categoryId"
      style="width: 100%"
    >
      <el-table-column
        prop="categoryName"
        tree-key="categoryId"
        width="600"
        :label="$t('publics.category')"
      />
      <el-table-column
        prop="pic"
        :label="$t('publics.image')"
        width="400px"
      >
        <template #default="scope">
          <img-show
            v-if="scope.row.grade !== 1"
            :src="scope.row.pic"
            :img-style="{'height': '100px','object-fit': 'contain'}"
          />
        </template>
      </el-table-column>
      <el-table-column
        prop="status"
        align="center"
        :label="$t('product.status')"
      >
        <template #default="scope">
          <span
            v-if="scope.row.status === 0"
            class="tag-text"
          >{{ $t('publics.offline') }}</span>
          <span
            v-else
            class="tag-text"
          >{{ $t('publics.normal') }}</span>
        </template>
      </el-table-column>
      <el-table-column
        prop="seq"
        align="center"
        :label="$t('hotSearch.seq')"
        min-width="60"
      />
      <el-table-column
        fixed="right"
        align="center"
        :label="$t('text.menu')"
        width="160"
      >
        <template #default="scope">
          <div class="text-btn-con">
            <div
              v-if="isAuth('prod:category:update')"
              class="default-btn text-btn"
              @click="onAddOrUpdate(scope.row.categoryId)"
            >
              {{ $t('groups.edit') }}
            </div>
            <div
              v-if="isAuth('prod:category:delete')"
              class="default-btn text-btn"
              @click="onDelete(scope.row.categoryId)"
            >
              {{ $t('text.delBtn') }}
            </div>
          </div>
        </template>
      </el-table-column>
    </el-table>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update
      v-if="addOrUpdateVisible"
      ref="addOrUpdateRef"
      @refresh-data-list="getDataList"
    />
  </div>
</template>

<script setup>
import AddOrUpdate from './add-or-update.vue'

import { treeDataTranslate, isAuth } from '@/utils'
import { ElMessage, ElMessageBox } from 'element-plus'

const dataList = ref([])
const dataListLoading = ref(false)
const categoryTableRef = ref(null)
// 获取数据列表
const getDataList = () => {
  dataListLoading.value = true
  http({
    url: http.adornUrl('/prod/category/list'),
    method: 'get',
    params: http.adornParams()
  }).then(({ data }) => {
    dataList.value = treeDataTranslate(data, 'categoryId', 'parentId')
    nextTick(() => {
      categoryTableRef.value?.doLayout()
    })
    dataListLoading.value = false
  })
}
onMounted(() => {
  getDataList()
})

// 新增 / 修改
const addOrUpdateRef = ref(null)
const addOrUpdateVisible = ref(false)
const onAddOrUpdate = id => {
  addOrUpdateVisible.value = true
  nextTick(() => {
    addOrUpdateRef.value.init(id)
  })
}

// 删除
const onDelete = id => {
  ElMessageBox.confirm(
    `${$t('sys.makeSure')}${$t('text.delBtn')}${$t('text.menu')}?`,
    $t('text.tips'),
    {
      confirmButtonText: $t('crud.filter.submitBtn'),
      cancelButtonText: $t('crud.filter.cancelBtn'),
      type: 'warning'
    }
  ).then(() => {
    http({
      url: http.adornUrl(`/prod/category/${id}`),
      method: 'delete',
      data: http.adornData()
    }).then(() => {
      ElMessage({
        message: $t('publics.operation'),
        type: 'success',
        duration: 1500,
        onClose: () => {
          getDataList()
        }
      })
    })
  })
}
</script>
<style lang="scss" scoped>
.page-category {
  :deep(.el-table__fixed-right) {
    height: 100% !important; //设置高优先，以覆盖内联样式
  }
  :deep(.el-table__fixed) {
    height: 100% !important; //设置高优先，以覆盖内联样式
  }
  :deep(.el-alert__content) {
    line-height: 1.5em;
  }
  .text-btn-con {
    width: 100%;
    display: flex;
    justify-content: space-evenly;
    .text-btn {
      margin-right: 0;
    }
  }
}
</style>
